package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.filesystem.File;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	
	import starling.core.Starling;
	import starling.events.Event;
	import starling.textures.Texture;
	import starling.utils.AssetManager;
	import starling.utils.RectangleUtil;
	import starling.utils.ScaleMode;
	import starling.utils.formatString;
	
	public class Mahjong extends Sprite
	{
		// 启动画面for 低画质
		[Embed(source="assets/system/startup.jpg")]
		private static var Background:Class;
		
		// 启动画面for 高画质
		[Embed(source="assets/system/startupHD.jpg")]
		private static var BackgroundHD:Class;
		
		private var _mStarling:Starling;
		private var _stageWidth:int;
		private var _stageHeight:int;
		
		public function Mahjong()
		{
			// 支持 autoOrient
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			_stageWidth  = Constants.STAGE_WIDTH;
			_stageHeight = Constants.STAGE_HEIGHT;
			var iOS:Boolean = Capabilities.manufacturer.indexOf("iOS") != -1;
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			Starling.multitouchEnabled = true;
			Starling.handleLostContext = !iOS;
			
			var viewPort:Rectangle = RectangleUtil.fit(
				new Rectangle(0, 0, _stageWidth, _stageHeight), 
				new Rectangle(0, 0, stage.fullScreenWidth, stage.fullScreenHeight), 
				ScaleMode.SHOW_ALL);
			
			var scaleFactor:int = viewPort.width < 480 ? 1 : 2; 
			var appDir:File = File.applicationDirectory;
			var assets:AssetManager = new AssetManager(scaleFactor);
			
			assets.verbose = Capabilities.isDebugger;
			assets.enqueue(                                                        //资源列表
				appDir.resolvePath("assets/audio"),
				appDir.resolvePath(formatString("assets/fonts/{0}x", scaleFactor)),
				appDir.resolvePath(formatString("assets/textures/{0}x", scaleFactor))
			);
			
			var background:Bitmap = scaleFactor == 1 ? new Background() : new BackgroundHD();
			Background = BackgroundHD = null; 
			background.x = viewPort.x;
			background.y = viewPort.y;
			background.width  = viewPort.width;
			background.height = viewPort.height;
			background.smoothing = true;
			addChild(background);
			
			_mStarling = new Starling(Root, stage, viewPort);
			_mStarling.stage.stageWidth  = _stageWidth;  
			_mStarling.stage.stageHeight = _stageHeight; 
			_mStarling.simulateMultitouch  = false;
			_mStarling.enableErrorChecking = Capabilities.isDebugger;
			
			_mStarling.addEventListener(Event.ROOT_CREATED, 
				function onRootCreated(event:Object, app:Root):void
				{
					_mStarling.removeEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
					removeChild(background);
					
					var bgTexture:Texture = Texture.fromBitmap(background, false, false, scaleFactor);
					
					app.start(bgTexture, assets);
					_mStarling.start();
				});
		}
		
	}
}